package cn.juque.jdfsystem.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.juque.common.base.PageInfo;
import cn.juque.common.constants.MessageEnum;
import cn.juque.common.disruptor.DisruptorHandler;
import cn.juque.common.exception.AppException;
import cn.juque.jdfsystem.disruptor.bo.SendMsgBodyBO;
import cn.juque.jdfsystem.disruptor.event.SendMsgDisruptorEventServiceImpl;
import cn.juque.jdfsystem.entity.ConfigInfo;
import cn.juque.jdfsystem.enums.HttpMessageTypeEnum;
import cn.juque.jdfsystem.enums.JdfSystemMsgEnum;
import cn.juque.jdfsystem.mapper.ConfigInfoMapper;
import cn.juque.jdfsystem.service.IConfigInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author JUQUE
 * @version 1.0.0
 * <ul>
 *  <li>simple-generator</li>
 *  <li>配置信息</li>
 * </ul>
 * @date 2023-01-08 14:38:52
 **/
@Service("configInfoService")
public class ConfigInfoServiceImpl implements IConfigInfoService {

    @Resource
    private ConfigInfoMapper configInfoMapper;

    @Resource
    private DisruptorHandler disruptorHandler;

    /**
     * 分页查询
     *
     * @param pageInfo   pageInfo
     * @param configInfo configInfo
     * @return DataGrid
     */
    @Override
    public List<ConfigInfo> listInfoWithPage(PageInfo pageInfo, ConfigInfo configInfo) {
        Page<ConfigInfo> page = new Page<>(pageInfo.getPage(), pageInfo.getLimit());
        List<ConfigInfo> list = this.configInfoMapper.queryInfoList(page, configInfo);
        pageInfo.setTotal(page.getTotal());
        return list;
    }

    /**
     * 查询
     *
     * @param configInfo configInfo
     * @return List
     */
    @Override
    public List<ConfigInfo> listInfoWithoutPage(ConfigInfo configInfo) {
        return this.configInfoMapper.queryInfoList(configInfo);
    }

    /**
     * 详情
     *
     * @param id 主键
     * @return ConfigInfo
     */
    @Override
    public ConfigInfo detail(String id) {
        return this.configInfoMapper.selectById(id);
    }

    /**
     * 保存
     *
     * @param configInfo configInfo
     */
    @Override
    public void saveInfo(ConfigInfo configInfo) {
        ConfigInfo query = new ConfigInfo();
        query.setConfigCode(configInfo.getConfigCode());
        Long count = this.configInfoMapper.selectCount(new QueryWrapper<>(query));
        if (SqlHelper.retBool(count)) {
            throw new AppException(JdfSystemMsgEnum.CONFIG_CODE_EXISTS);
        }
        boolean flag = SqlHelper.retBool(this.configInfoMapper.insert(configInfo));
        if (!flag) {
            throw new AppException(MessageEnum.SAVE_FAIL);
        }
        this.sendMessage();
    }

    /**
     * 更新
     *
     * @param configInfo configInfo
     */
    @Override
    public void updateInfo(ConfigInfo configInfo) {
        boolean flag = SqlHelper.retBool(this.configInfoMapper.updateById(configInfo));
        if (!flag) {
            throw new AppException(MessageEnum.UPDATE_FAIL);
        }
        this.sendMessage();
    }

    /**
     * 删除
     *
     * @param ids 主键列表
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteInfo(List<String> ids) {
        if (CollUtil.isEmpty(ids)) {
            return;
        }
        int count = this.configInfoMapper.deleteBatchIds(ids);
        if (count != ids.size()) {
            throw new AppException(MessageEnum.DELETE_FAIL);
        }
        this.sendMessage();
    }

    /**
     * 发送消息
     */
    private void sendMessage() {
        SendMsgBodyBO<?> sendMsgBodyBO = new SendMsgBodyBO<>();
        sendMsgBodyBO.setMessageTypeEnum(HttpMessageTypeEnum.CONFIG_CHANGE);
        sendMsgBodyBO.setBusinessEventService(SendMsgDisruptorEventServiceImpl.BEAN);
        this.disruptorHandler.publish(sendMsgBodyBO);
    }

}
